package com.fbt.transfer.app.driver.util;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * @author tuocoo
 */
public class UserManager {

  private static final String PREF_NAME = "user_prefs";
  private static final String KEY_TOKEN = "token";
  private static final String KEY_USER_ID = "user_id";
  private static final String KEY_USER_NAME = "user_name";
  private static final String KEY_VOLUME = "volume";
  private static final String KEY_FLOATING_WINDOW_ENABLED = "floating_window_enabled";
  private static final String KEY_ENVIRONMENT = "environment";
  public static final String ENV_PRODUCTION = "production";
  public static final String ENV_TEST = "test";
  private static final String KEY_CHECKUPDATE = "checkUpdate";
  private static final String KEY_IS_LOGIN = "is_login";
  private static UserManager instance;
  private SharedPreferences prefs;

  private UserManager (Context context) {
    prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
  }

  public static UserManager getInstance (Context context) {
    if (instance == null) {
      instance = new UserManager(context.getApplicationContext());
    }
    return instance;
  }

  /**
   * 检查是否已登录
   *
   * @return 检查是否已登录
   */
  public boolean isLoggedIn () {
    return prefs.getBoolean(KEY_IS_LOGIN, false);
  }

  /**
   * 保存用户登录信息
   *
   * @param token    token
   * @param userId   用户ID
   * @param username 登陆名
   */
  public void saveUserInfo (String token, String userId, String username) {
    prefs.edit()
        .putString(KEY_TOKEN, token)
        .putString(KEY_USER_ID, userId)
        .putBoolean(KEY_IS_LOGIN, true)
        .putString(KEY_USER_NAME, username)
        .apply();
  }

  /**
   * 清除用户信息（登出时使用）
   */
  public void clearUserInfo () {
    prefs.edit()
        .remove(KEY_TOKEN)
        .remove(KEY_USER_ID)
        .putBoolean(KEY_IS_LOGIN, false)
        .remove(KEY_USER_NAME)
        .apply();
  }

  /**
   * 获取用户名
   *
   * @return 获取用户名
   */
  public String getUsername () {
    return prefs.getString(KEY_USER_NAME, Constants.EMPTY_STRING);
  }

  /**
   * 获取token
   *
   * @return token
   */
  public String getToken () {
    return prefs.getString(KEY_TOKEN, null);
  }

  /**
   * 获取用户ID
   *
   * @return 获取用户ID
   */
  public String getUserId () {
    return prefs.getString(KEY_USER_ID, null);
  }

  /**
   * 获取用户名
   *
   * @return 获取用户名
   */
  public String getUserName () {
    return prefs.getString(KEY_USER_NAME, null);
  }

  public void saveToken (String token) {
    prefs.edit().putString(KEY_TOKEN, token).apply();
  }

  public void saveUserId (String userId) {
    prefs.edit().putString(KEY_USER_ID, userId).apply();
  }

  public void saveUserName (String userName) {
    prefs.edit().putString(KEY_USER_NAME, userName).apply();
  }

  public void setVolume (int volume) {
    prefs.edit().putInt(KEY_VOLUME, volume).apply();
  }

  /**
   * 获取音量控制大小，为0则不出声
   *
   * @return 音量控制大小
   */
  public int getVolume () {
    return prefs.getInt(KEY_VOLUME, 50); // 默认音量50%
  }

  public void setFloatingWindowEnabled (boolean enabled) {
    prefs.edit().putBoolean(KEY_FLOATING_WINDOW_ENABLED, enabled).apply();
  }

  public boolean isFloatingWindowEnabled () {
    return prefs.getBoolean(KEY_FLOATING_WINDOW_ENABLED, true);
  }

  public void setEnvironment (boolean isTest) {
    prefs.edit()
        .putString(KEY_ENVIRONMENT, isTest ? ENV_TEST : ENV_PRODUCTION)
        .apply();
  }

  /**
   * 默认使用生产环境
   */
  public String getEnvironment () {
    return prefs.getString(KEY_ENVIRONMENT, ENV_TEST);
  }

  public boolean isTestEnvironment () {
    return ENV_TEST.equals(getEnvironment());
  }

  /**
   * 最后一次更新时间戳
   *
   * @return 最后一次更新时间戳
   */
  public long getLastCheckUpDate () {
    return prefs.getLong(KEY_CHECKUPDATE, System.currentTimeMillis() - 1000 * 60 * 10);
  }

  public void saveLastCheckUpDate () {
    SharedPreferences.Editor editor = prefs.edit();
    editor.putLong(KEY_CHECKUPDATE, System.currentTimeMillis() + 1000 * 60 * 60 * 4);
    editor.apply();
  }

}
